package com.frogs.ucenter.service.impl;

import java.util.Arrays;

import org.apache.commons.lang.ArrayUtils;
import org.jsoup.Connection.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.frogs.system.util.ApiUrlParamEnum;
import com.frogs.system.util.HotIndexTypeEnum;
import com.frogs.system.web.api.entity.ApiUrl;
import com.frogs.system.web.api.response.Response.ApiResultEnum;
import com.frogs.system.web.api.response.Response.ResultParamEnum;
import com.frogs.ucenter.service.GoodsService;

@Service("goodsService")
public class GoodsServiceImpl extends GoodsBaseServiceImpl implements GoodsService {
	private Logger log = LoggerFactory.getLogger(GoodsServiceImpl.class);

	/**
	 * 根据标签推荐
	 */
	@Override
	public JSONObject recommendByTags(String tags,Integer pageSize , Integer pageNo) {
		ApiUrl apiUrl = this.buildMallApiBaseUrl("MALL_GOODS_RECOMMEND");

		String[] otherParams = {ApiUrlParamEnum.tags.getParamName(),tags,
				ApiUrlParamEnum.pageSize.getParamName(),pageSize.toString(),
				ApiUrlParamEnum.pageNo.getParamName(),pageNo.toString()
		};
		
		String[] allParams = (String[])ArrayUtils.addAll(apiUrl.getKeyvals(), otherParams);
		JSONObject result = null;
		try{
			result = (JSONObject)this.requestApi(apiUrl.getUrl(), Method.POST, allParams);
			if (ApiResultEnum.SUCCESS.getId() != result.getJSONObject("response").getIntValue(ResultParamEnum.code.getParamName()))
				throw new IllegalStateException(result.getJSONObject("response").getString(ResultParamEnum.tip.getParamName()));
		} catch (Exception e) {
			log.error("func[recommend] url[{}] params[{}] exception[{} - {}] desc[fail]",
					new Object[] { apiUrl.getUrl(),  Arrays.deepToString(allParams) , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return result;
	}
	
	@Override
	public JSONArray getRecommendGoods(Integer userId, Integer size) {
		JSONArray array = new JSONArray();
		try {
			/*ResponseMessage res = null;
			if (userId < 1 || userId == null) {
				res = TJBRecommendIceClient.getHotIndexListByType(HotIndexTypeEnum.BY_VIEW, "null", -1, -1, size);
			} else {
				res = TJBRecommendIceClient.getHotIndexListByType(HotIndexTypeEnum.BY_VIEW, userId.toString(), -1, -1, size);
			}
			String str = JSONObject.toJSONString(res);
			JSONObject result = JSONObject.parseObject(str);
			if (result == null) {
				log.error("func[getRecommendGoods] userId[{}] size[{}] str[{}] desc[fail]",
						new Object[] { userId,  size , str });
				return array;
			}
			if ("200".equals(result.getString(ResultParamEnum.code.getParamName()))) {
				String data = result.getString("data");
				JSONObject dataObject = JSONObject.parseObject(data);
				JSONArray topn = dataObject.getJSONArray("topn");
				return topn;
			}*/
			return null;
		} catch (Exception e) {
			log.error("func[getRecommendGoods] userId[{}] size[{}] exception[{} - {}] desc[fail]",
					new Object[] { userId,  size , e.getMessage(), Arrays.deepToString(e.getStackTrace()) });
		}
		return array;
	}
	
}
